import { ref } from 'vue'
import { defineStore } from 'pinia'
import {userLoginAPI,exitLoginAPI} from '@/API/user'
import { showToast } from 'vant'
import { useRouter } from 'vue-router'

export const useUserStore = defineStore('user', () => {
  const info = ref<any>({
    userid:''
    // token:''
  })

  const router = useRouter()
  //登录
const onLogin =async (account:any,password:any) => {
  const res:any=await userLoginAPI(account, password)
  showToast(res.data.message)
  if(res.data.status===200){
    info.value.token = res.data.token
   setTimeout(() => router.push('/'),3000)
  }
}
//退出
const exitLogin = async()=>{
  await exitLoginAPI()
  info.value={}
  router.replace('/login')
}



  return {
    info,
    onLogin,
    exitLogin,
    
   }
},
{
  persist: true,
})
